<?php

namespace app\home\controller;

use think\Controller;

class Index extends Controller
{
    protected $BusinessModel;
    protected $SubjectModel;
    protected $CategoryModel;
    public function _initialize()
    {
        // 如果用助手调用多级模型 例如：路径：app\common\model\Business\Business
        $this->BusinessModel = model('Business.Business');
        $this->SubjectModel = model('Subject.Subject');
        $this->CategoryModel = model('Subject.Category');
    }
    // 前端首页
    public function index()
    {   
        /*
            OrderRaw: 自定义SQL函数
            LPAD(str, len, pad_str)
            str：要进行填充的字符串。
            len：指定最终字符串的总长度。
            pad_str：用于填充的字符串。
        */ 
        $recommendlist = $this->SubjectModel->OrderRaw('LPAD(LOWER(likes),10,0) DESC')->limit(5)->select();

     
        // 查询所有的课程
        $subjectlist = $this->SubjectModel->order('createtime DESC')->select();
        $this->assign([
            'active1'=>'mui-active',
            'recommendlist' => $recommendlist,
            'subjectlist' => $subjectlist,
        ]);
        return $this->fetch();
    }
    // 登录
    public function login()
    {
        if ($this->request->isPost()) {
            $mobile = $this->request->param('mobile', '', 'trim');
            $result = $this->BusinessModel->where('mobile', $mobile)->find();
            if (!$result) {
                $this->error('没有该用户');
            } else {
                $pwd = $result['password'];
                $password = $this->request->param('password', '', 'trim');
                $salt = $result['salt'];
                $password = md5($password . $salt);
                if ($pwd == $password) {
                    // 存储用户信息
                    $data = [
                        'id' => $result['id'],
                        'mobile' => $result['mobile'],
                        'nickname' => $result['nickname'],
                        'avatar' => $result['avatar_text'],
                        'gender' => $result['gender'],
                        'province' => $result['province'],
                        'city' => $result['city'],
                        'district' => $result['district'],
                        'createtime' => $result['createtime'],
                        'money' => $result['money'],
                        'email' => $result['email']
                    ];
                    session('LoginUser', $data);
                    $this->success('登录成功', 'home/business/index');
                } else {
                    $this->error('密码错误！');
                }
            }
        }

        // 临时关闭当前模板的布局功能
        $this->view->engine->layout(false);
        return $this->fetch();
    }
    // 注册
    public function register()
    {
        if ($this->request->isPost()) {
            $mobile = $this->request->param('mobile', '', 'trim');
            $password = $this->request->param('password', '', 'trim');
            $salt = randstr();
            $password = md5($password . $salt);
            $data = [
                'password' => $password,
                'mobile' => $mobile,
                'salt' => $salt,
                'nickname' => $mobile
            ];
            $result = $this->BusinessModel->validate('common/Business/Business')->save($data);
            if (!$result) {
                // getError => 返回模型的错误信息
                $this->error($this->BusinessModel->getError());
            } else {
                $this->success('注册成功', 'home/business/login');
            }
        }
        // 临时关闭当前模板的布局功能
        $this->view->engine->layout(false);
        return $this->fetch();
    }
    // 退出
    public function logout(){
         // 清除
         session('LoginUser', null);
         $this->success('退出成功');
    }
}
